package com.example.xinli;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.xinli.fragment.HomeFragment;
import com.example.xinli.fragment.CommunityFragment;
import com.example.xinli.fragment.InterventionFragment;
import com.example.xinli.fragment.MeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;



public class UserActivity extends AppCompatActivity {

    List<Fragment> list;
    BottomNavigationView bottomNavigation;
    private String loggedInUsername; // 保存当前登录用户名


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        loggedInUsername = getIntent().getStringExtra("username");


        bottomNavigation = findViewById(R.id.bottomNavigation);

        list = new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new CommunityFragment());
        list.add(new InterventionFragment());
        list.add(new MeFragment());

        showFragment(list.get(0));

        bottomNavigation.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.menu_home:
                        showFragment(list.get(0));
                        break;
                    case R.id.menu_emotion:
                        showFragment(list.get(1));
                        break;
                    case R.id.menu_intervention:
                        showFragment(list.get(2));
                        break;
                    case R.id.menu_me:
                        MeFragment meFragment = (MeFragment) list.get(3);
                        meFragment.setCurrentUser(loggedInUsername);
                        showFragment(meFragment);
                        break;
                }
                return true;
            }
        });




    }

    private void showFragment(Fragment fragment){

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container,fragment);
        ft.commit();
    }
}

